var gulp = require("gulp");
var connect = require("gulp-connect");
var concat = require("gulp-concat");
var scripts = require("./conf/index").scripts;
var sass = require('gulp-sass');
sass.compiler = require('node-sass');
var findSync = require("./utils/index").findSync;
var scriptsPathArray = null;
var scriptsTaskAraay = [];
var scssTestarrey = [];
var imagemin = require("gulp-imagemin");
var scssjson = require("./conf/index").scss;

var proxyList = require("./conf/index").proxyList;

// 引入代理服务器
var proxy = require('http-proxy-middleware');
gulp.task("html", function () {
    gulp.src("./src/*.html").pipe(gulp.dest("./dev")).pipe(connect.reload());
})
function gpproxy(proxyList) {
    let result = [];
    for (let attr in proxyList) {
        result.push(proxy(attr, {
            // 像nginx那样去配
            target: proxyList[attr].src,//代理的目标路径
            changeOrigin: true,
            //路径重写规则
            // 将key值变成一个参数传进来，必须[attr]----成为字符串
            pathRewirte:!proxyList[attr].rewrite ?{[attr]:""} :{}

        }))
    }
    return result;
    // [
    //     proxy('/pxx',{
    //         // 像nginx那样去配
    //         target:'https://apiv2.pinduoduo.com/api/fiora/subject/goods/',//代理的目标路径
    //         changeOrigin:true,
    //         pathRewirte:{//路径重写规则
    //             '^/api':''
    //         }
    //     })
    // ]
}
gulp.task("connect", function () {
    connect.server({
        root: "dev",
        livereload: "true",
        middleware: function (connect, opt) {
            return gpproxy(proxyList);
        }
    });
})
function watchScripts(json) {
    for (let attr in json) {
        gulp.watch(json[attr].src + "/*.js", ["scripts-" + attr])
    }
}
function watchScss(json) {
    for (let attr in json) {
        gulp.watch(json[attr].src + "/*.scss", ["scss-" + attr])
    }
}
gulp.task("watch", function () {
    gulp.watch("./src/*.html", ["html"]);
    watchScripts(scripts);
    gulp.watch("./src/scss/*/*.scss", ["scss"]);
    watchScss(scssjson);
})
function mergeMysctipts(json) {
    for (let attr in json) {
        scriptsTaskAraay.push("scripts-" + attr);
        gulp.task("scripts-" + attr, function () {
            return gulp.src([scripts[attr].src] + "/*.js")
                .pipe(concat(attr + ".js"))
                .pipe(gulp.dest("./dev/scripts"))
                .pipe(connect.reload());
        })
    }
}
gulp.task('scss', function () {
    return gulp.src('./src/scss/index/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('./dev/stylesheets'));
});
function mergeMyscss(json) {
    for (let attr in json) {
        scssTestarrey.push("scss-" + attr);
        gulp.task("scss-" + attr, function () {
            return gulp.src([json[attr].src] + "/*.scss")
                .pipe(concat(attr + ".scss"))
                .pipe(sass().on('error', sass.logError))
                .pipe(gulp.dest("./dev/stylesheets"))
                .pipe(connect.reload());
        })
    }
}
function init() {
    scriptsPathArray = findSync("./src/scripts").map(path => {
        return "./" + path.replace(/\\/g, "/");
    })
    // let scriptsUnSaveArray = ["!./src/scripts/common/*.js"];
    // scriptsPathArray = scriptsPathArray.concat(scriptsUnSaveArray);
    mergeMysctipts(scripts);
    mergeMyscss(scssjson);
}

init();


gulp.task("scss", scssTestarrey);
gulp.task("scripts", scriptsTaskAraay);
gulp.task("images", function () {
    return gulp.src("./src/images/**/*")
        .pipe(gulp.dest("./dev/images"))
})

gulp.task("gulp-dev", ["html", "scripts", "scss", "images", "watch", "connect"]);

require("./gulp-build");